package com.zreal.service.${packageName};

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ${packageName}.entity.${table.className}Entity;
import ${packageName}.mapper.${table.className}Mapper;


@Service
public class ${table.className}ServiceImpl {

	@Autowired
	private ${table.className}Mapper ${table.className?uncap_first}Mapper;
	
	/**
	 * 新增
	 *
	 * @param ${table.className?uncap_first} 实体
	 * @return Long
	 */
	public Long insert${table.className}(${table.className}Entity ${table.className?uncap_first}){
		return ${table.className?uncap_first}Mapper.insert(${table.className?uncap_first});
	}
	
	/**
	 * 更新
	 *
	 * @param ${table.className?uncap_first} 实体
	 */
	public void update${table.className}(${table.className}Entity ${table.className?uncap_first}){
		${table.className?uncap_first}Mapper.update(${table.className?uncap_first});
	}
	
	/**
	 * 根据条件更新
	 *
	 * @param ${table.className?uncap_first} 实体
	 */
	public void update${table.className}ByExample(${table.className}Entity ${table.className?uncap_first},SqlCriteria sqlCriteria){
		${table.className?uncap_first}Mapper.updateByExample(${table.className?uncap_first},sqlCriteria);
	}
	
	/**
	 * 保存
	 *
	 * @param ${table.className?uncap_first} 实体
	 */
	public void save${table.className}(${table.className}Entity ${table.className?uncap_first}){
	    if(${table.className?uncap_first}.getId() == null || "".equals(${table.className?uncap_first}.getId())) {
            this.insert${table.className}(${table.className?uncap_first});
        } else {
            this.update${table.className}(${table.className?uncap_first});
        }
	}
	
	/**
	 * 根据条件查询
	 *
	 * @param sqlCriteria 查询条件
	 * @return List<${table.className}Entity>
	 */
	public List<${table.className}Entity> query${table.className}List(SqlCriteria sqlCriteria){
		return ${table.className?uncap_first}Mapper.selectByExample(sqlCriteria);
	}
	
	/**
	 * 根据条件分页查询
	 *
	 * @param sqlCriteria 查询条件
	 * @param pageNo 当前页
	 * @param pageSize 页大小
	 * @return Page<${table.className}Entity>
	 */
	public Page<${table.className}Entity> queryPage${table.className}(SqlCriteria sqlCriteria, int pageNo, int pageSize){
		PageHelper.startPage(pageNo, pageSize);
	    Page<${table.className}Entity> page = ${table.className?uncap_first}Mapper.selectPageByExample(sqlCriteria);
		return page;
	}
	
	/**
	 * 批量插入
	 *
	 * @param ${table.className?uncap_first}s 实体集合
	 */
	public void insert${table.className}s(List<${table.className}Entity> ${table.className?uncap_first}s){
		for (${table.className}Entity entity : ${table.className?uncap_first}s) {
			${table.className?uncap_first}Mapper.insert(entity);
		}
	}
	
	/**
	 * 批量更新
	 *
	 * @param ${table.className?uncap_first}s 实体集合
	 */
	public void update${table.className}s(List<${table.className}Entity> ${table.className?uncap_first}s){
		for (${table.className}Entity entity : ${table.className?uncap_first}s) {
			${table.className?uncap_first}Mapper.update(entity);
		}
	}
	
	/**
	 * 根据条件查询
	 *
	 * @param id id
	 * @return List<${table.className}Entity>
	 */
	public ${table.className}Entity get${table.className}ById(String id){
		return ${table.className?uncap_first}Mapper.getEntityById(id);
	}
	
	/**
	 * 根据条件查询
	 *
	 * @param id id
	 * @return Map<String,Object>
	 */
	public Map<String,Object> getMap${table.className}ById(String id){
		return (Map<String,Object>)${table.className?uncap_first}Mapper.getEntityMapById(id);
	}
	
	/**
	 * 根据id删除
	 *
	 * @param id id
	 * @return Map<String,Object>
	 */
	public void del${table.className}ById(String id){
		${table.className?uncap_first}Mapper.deleteById(id);
	}
	
	/**
	 * 根据条件删除
	 *
	 * @param sqlCriteria 条件
	 * @return Map<String,Object>
	 */
	public void del${table.className}ByExample(SqlCriteria sqlCriteria){
		${table.className?uncap_first}Mapper.deleteByExample(sqlCriteria);
	}
	<#--
	public Page getMapPage${table.className}(SqlCriteria sqlCriteria, int pageNo, int pageSize){
		return ${table.className?uncap_first}Mapper.queryForPageByCriteriaWithSqlId(${table.className?uncap_first}Mapper.getEntityName() + ".selectMapByExample", sqlCriteria, pageNo, pageSize);
	}
	-->
	
	/**
	 * 根据条件统计数量
	 *
	 * @param id id
	 * @return Long
	 */
	public Long count${table.className}(SqlCriteria sqlCriteria){
		return ${table.className?uncap_first}Mapper.countByExample(sqlCriteria);
	}
	
	/**
     * 是否存在
     * 
     * @param sqlCriteria 条件
     * @return boolean
     */
    public boolean exists${table.className}(SqlCriteria sqlCriteria) {
        return ${table.className?uncap_first}Mapper.countByExample(sqlCriteria) > 0;
    }
}
